package com.example.redislock.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description:
 * @Author: dengping.xu
 * @Data: 2021-08-18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderDetail extends OrderId implements Serializable {
    private String price;

    @Override
    public String toString() {
        return "OrderDetail{" +
                "price='" + price + '\'' +
                "} " + super.toString();
    }

    public static void main(String[] args) {
//        OrderDetail orderDetail = new OrderDetail("12");
//        orderDetail.setId("1");
//        String jsonStr = JSONUtil.toJsonStr(orderDetail);
//        OrderId detail = JSONUtil.toBean(jsonStr,OrderDetail.class);
//        OrderDetail detail1 = new OrderDetail();
//
//        if(detail instanceof OrderDetail){
//            System.out.println("属于OrderDetail");
//        }
//        /*if(detail instanceof OrderDetail){
//            detail1 = (OrderDetail) detail;
//        }*/
//        BeanUtil.copyProperties(detail,detail1);
//        System.out.println(detail1);
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }
//        List<Integer> sum = Collections.synchronizedList(new ArrayList<>());
        List<Integer> sum = new ArrayList<>();
        long beforeTime = System.currentTimeMillis();
        /*list.forEach(item-> {
            try {
                sum.add(testGetCount(item));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });*/
        list.parallelStream().forEach(item-> {
            try {
                Integer integer = testGetCount(item);
                synchronized (sum){
                    sum.add(integer);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Collections.sort(sum);
        System.out.println(sum);
        System.out.println("-----------");
        System.out.println(System.currentTimeMillis()-beforeTime);
    }

    private static Integer testGetCount(Integer item) throws InterruptedException {
        Thread.sleep(100);
        return item;
    }
}
